package com.sky.service.impl;


import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    //注入套餐mapper
    @Autowired
    SetmealMapper setmealMapper;
    //注入dishmmaper
    @Autowired
    DishMapper dishMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO shoppingCartDTO
     *
     */
    @Override
    public void insertShoping(ShoppingCartDTO shoppingCartDTO) {
        //创建一个购物车对象。
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //根据属性查询购物车中的数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //动态条件查询购物车中的数据，返回一个购物车集合。
        List<ShoppingCart> list = shoppingCartMapper.selectCartByshopping(shoppingCart);
        //判断集合，如果为空的话，就直接新增
        if (list == null || list.size() ==0){
            //新增之前先进行数据填充
            Long dishId = shoppingCart.getDishId();
            Long setmealId = shoppingCart.getSetmealId();
            if (dishId == null){
                //加入购物车的是套餐
                //查询套餐表
                Setmeal setmeal = setmealMapper.getById(setmealId);
                //数据填充

                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());

            }else {
                //加入的是菜品
                //查询菜品表
                List<Long> ids = new ArrayList<>();
                ids.add(dishId);
                List<Dish> dishes = dishMapper.selectDishByIds(ids);
                Dish dish = dishes.get(0);
                //补充数据
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
            }
            //公共字段的填充
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            //将新的商品数据插入购物车。
            shoppingCartMapper.insertShoppingIntoCart(shoppingCart);
        }
        else {
            //能查出数据，说明购物车中有数据
            //但由于同一个用户的同类商品唯一，故只有一条
            //取出该条商品数据
            ShoppingCart shoppingCartOld = list.get(0);
            //对该数据的数量进行修改。
            shoppingCartOld.setNumber(shoppingCartOld.getNumber()+1);
            shoppingCartMapper.updateShoppingNumberById(shoppingCartOld);


        }




    }


    @Override
    public List<ShoppingCart> selectAllShoppings() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.selectCartByshopping(shoppingCart);
        return list;
    }

    @Override
    public void deleteAllShoppings() {
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteAllShoppings(currentId);

    }

    @Override
    public void subShopping(ShoppingCartDTO shoppingCartDTO) {
        //创建一个购物车对象。
        ShoppingCart shoppingCart = new ShoppingCart();
        //属性拷贝
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //根据属性查询购物车中的数据
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        //动态条件查询购物车中的数据，返回一个购物车集合。
        List<ShoppingCart> list = shoppingCartMapper.selectCartByshopping(shoppingCart);
        //获取集合中的元素
        ShoppingCart shoppingCartOld = list.get(0);
        //判断商品的数量
        Integer number = shoppingCartOld.getNumber();
        if (number==1){
            shoppingCartMapper.deleteShoppingById(shoppingCartOld.getId());
        }else {
            shoppingCartOld.setNumber(shoppingCartOld.getNumber()-1);
            shoppingCartMapper.updateShoppingNumberById(shoppingCartOld);
        }
    }


}
